package com.coder.oj.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.coder.oj.admin.dto.RoleDto;
import com.coder.oj.admin.dto.RoleOauthDto;
import com.coder.oj.admin.mapstruct.RoleMapstruct;
import com.coder.oj.admin.vo.OauthVo;
import com.coder.oj.admin.vo.RoleVo;
import com.coder.oj.common.core.JsonPage;
import com.coder.oj.common.entity.AdminRole;
import com.coder.oj.common.entity.Role;
import com.coder.oj.common.entity.RoleOauth;
import com.coder.oj.common.enums.CommonStatusEnum;
import com.coder.oj.common.exception.DataException;
import com.coder.oj.common.mapper.AdminMapper;
import com.coder.oj.common.mapper.AdminRoleMapper;
import com.coder.oj.common.mapper.RoleMapper;
import com.coder.oj.common.mapper.RoleOauthMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Bosen 2022/11/25 11:26
 **/
@Service
public class RoleService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private AdminRoleMapper adminRoleMapper;

    @Autowired
    private RoleMapstruct roleMapstruct;

    @Autowired
    private RoleOauthMapper roleOauthMapper;

    public JsonPage<RoleVo> getRoleList() {
        List<RoleVo> collect = roleMapper.selectList(Wrappers.emptyWrapper()).stream().map(entity -> {
            RoleVo vo = roleMapstruct.toVO(entity);
            vo.setCreateBy(adminMapper.getAdminName(entity.getCreateBy()));
            vo.setUpdateBy(adminMapper.getAdminName(entity.getUpdateBy()));
            return vo;
        }).collect(Collectors.toList());
        return new JsonPage<>(collect);
    }
    
    /**
     * 修改角色 
     * @author Bosen 2022/12/27 11:16
     **/
    @Transactional(rollbackFor = Exception.class)
    public void updateRole(RoleDto params) {
        roleMapper.updateById(roleMapstruct.toEntity(params));
        // 删除角色原有权限
        roleOauthMapper.deleteByRoleId(params.getId());
        // 新增权限
        for(Long oauthId : params.getOauthIds()) {
            RoleOauth roleOauth = new RoleOauth();
            roleOauth.setRoleId(params.getId());
            roleOauth.setOauthId(oauthId);
            roleOauthMapper.insert(roleOauth);
        }
    }

    /**
     * 删除角色
     * @author Bosen 2022/12/27 11:19
     **/
    @Transactional(rollbackFor = Exception.class)
    public void deleteRoleById(Long id) {
        LambdaQueryWrapper<AdminRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdminRole::getRoleId, id);
        if(adminRoleMapper.selectCount(queryWrapper) > NumberUtils.INTEGER_ZERO) {
            throw new DataException("该角色已被绑定");
        }
        roleMapper.deleteById(id);
        roleOauthMapper.deleteByRoleId(id);
    }

    /**
     * 管理员角色id集合
     * @author Bosen 2022/12/31 1:09
     */
    public List<String> getRoleIdsByAdminId(Long id) {
        List<AdminRole> adminRoles = new LambdaQueryChainWrapper<>(adminRoleMapper)
                .eq(AdminRole::getAdminId, id).list();
        return adminRoles.stream().map(
                item -> String.valueOf(item.getRoleId())
        ).collect(Collectors.toList());
    }
}
